package com.collectplus.express.menu;

import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppCache;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.UserBean;

import droid.frame.utils.http.BitmapUtils;
import droid.frame.utils.lang.StringReplaceUtil;

/**
 * 个人二维码
 *
 * @author xiaoyang<br>
 *         2017-12-13上午10:18:33
 */
public class MyQRCodeActivity extends BaseActivity2 {

    private ImageView mQRcodeIV;

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_my_qrcode);
        super.findViewById();
        getAppTitle().setCommonTitle("我的二维码");
        mQRcodeIV = (ImageView) findViewById(R.id.qrcode_iv);
        UserBean userBean = AppCache.getUser();
        ((TextView) findViewById(R.id.user_name_tv)).setText(userBean.getName());
        if (isNotEmpty(userBean.getPhone())) {
            ((TextView) findViewById(R.id.user_mobile_tv)).setText(StringReplaceUtil.idCardReplaceWithStar(userBean.getPhone(), 3, 4, "*"));
        }
        if (isNotEmpty(userBean.getQrCodeUrl())) {
            mQRcodeIV.setImageBitmap(BitmapUtils.base64ToBitmap(userBean.getQrCodeUrl()));
        } else {
            showLoadingDialog(null);
            AppHttp.getInstance().getUserQRcode();
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.get_user_qrcode:
                cancelLoadingDialog();
                AppResult<?> result = JSON.parser(msg.obj);
                if (result.isSuccess()) {
                    final String codeStr = (String) result.getResult();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mQRcodeIV.setImageBitmap(BitmapUtils.base64ToBitmap(codeStr));
                            UserBean userBean = AppCache.getUser();
                            userBean.setQrCodeUrl(codeStr);
                            AppCache.setUser(userBean);
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }

    }
}
